import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { SPetArenaChallengeEnd } from "../../../../../Message/Game/SPetArenaChallengeEnd";
import { Cache } from "../../../cache/Cache";
import { ItemData } from "../../../resource/info/ItemData";
import { PetCpnRewardUtil } from "./info/PetCpnRewardUtil";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { SWFPlayer } from "../../../scene/modle/SWFPlayer";
import { ModelType } from "../../../scene/modle/data/ModelType";
import { ResFileConst } from "../../../resource/ResFileConst";
import { ObjCreate } from "../../common/ObjCreate";
import { Game } from "../../../Game";
import { GImageBitmap } from "../../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../../resource/ImagesConst";
import { PetExploreMainAvatar } from "../explore/PetExploreMainAvatar";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { GameController } from "../../../mvc/GameController";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { SEntityInfo } from "../../../../../Message/BroadCast/SEntityInfo";
import { EntityUtil } from "../../../scene/layer/utils/EntityUtil";
import { PlayerEvent } from "../../../scene/events/PlayerEvent";
import { EEntityType } from "../../../../../Message/Public/EEntityType";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
import { IEntity } from "../../../scene/player/IEntity";
type int = number;
//class PetCpnBattleWindow
    
    export  class PetCpnBattleWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this.setSize(840, 550);
            this.titleHeight = 28;
            this.title = Language.getString(54650);
            return;
        }

        public updateBattleResult(arg1: SPetArenaChallengeEnd): void
        {
            var loc1=0;
            var loc2=false;
            var loc3=null;
            var loc4=null;
            if (arg1) 
            {
                loc1 = Cache.instance.petCompetition.petArenaExLv;
                loc2 = arg1.success;
                loc4 = "";
                if (!this._zfItemData) 
                {
                    this._zfItemData = new ItemData(14077600);
                }
                if (loc2) 
                {
                    loc3 = "PetCpnBattleSuccess.swf";
                    if (arg1.rewards) 
                    {
                        loc4 = Language.getString(54675) + PetCpnRewardUtil.getStrByRewardArray(arg1.rewards);
                        if (Cache.instance.role.entityInfo.level >= 80) 
                        {
                            loc4 = loc4 + ("、" + Language.getString(54734));
                        }
                        if (loc1 >= 65 && loc1 <= 69) 
                        {
                            loc4 = loc4 + ("、" + ItemsUtil.getItemName(this._zfItemData));
                        }
                        else if (loc1 >= 70) 
                        {
                            loc4 = loc4 + ("、" + Language.getString(54712));
                        }
                    }
                    this._rewardTxt.htmlText = loc4;
                    this._rewardSprite.visible = true;
                }
                else 
                {
                    loc3 = "PetCpnBattleFailure.swf";
                    if (loc1 >= 65 && loc1 <= 69) 
                    {
                        loc4 = loc4 + (Language.getString(54675) + ItemsUtil.getItemName(this._zfItemData));
                    }
                    else if (loc1 >= 70) 
                    {
                        loc4 = loc4 + (Language.getString(54675) + Language.getString(54712));
                    }
                    this._rewardTxt.htmlText = loc4;
                    this._rewardSprite.visible = true;
                }
                if (this._effectPlayer == null) 
                {
                    this._effectPlayer = new SWFPlayer();
                }
                this._effectPlayer.load(loc3, ModelType.NormalSwf, null);
                this._effectPlayer.move(430, 210);
                if (!this.contains(this._effectPlayer)) 
                {
                    this.addChild(this._effectPlayer);
                }
                this._hideBattleBtn.styleName = ResFileConst.PetCpnBack;
            }
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            ObjCreate.createBg(17, 39, 805, 500, this);
            this._mapBitmap = new Object /* flash.display.Bitmap */();
            this._mapBitmap.x = 21;
            this._mapBitmap.y = 41;
            this.addChild(this._mapBitmap);
            Game.petFight.addToStage(this);
            Game.petFight.x = 21;
            Game.petFight.y = 41;
            this.addChild(new GImageBitmap(ImagesConst.PetCpnVS, 380, 50));
            this._petAvatar = new PetExploreMainAvatar();
            this._petAvatar.showViewBtn(false);
            this._petAvatar.x = 25;
            this._petAvatar.y = 45;
            this.addChild(this._petAvatar);
            this._enemyAvatar = new PetExploreMainAvatar();
            this._enemyAvatar.isScale = true;
            this._enemyAvatar.showViewBtn(false);
            this._enemyAvatar.x = 620;
            this._enemyAvatar.y = 45;
            this.addChild(this._enemyAvatar);
            this._levelTxt = ObjCreate.createTextField("", 0, 50, 695, 30, this, new GTextFormat(FontUtil.songtiName, 14, 15788607));
            this._levelTxt.filters = [FilterConst.vipGlowFilter];
            this._levelTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._nameTxt = ObjCreate.createTextField("", 0, 75, 695, 30, this, new GTextFormat(FontUtil.songtiName, 14, 15788607));
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._hideBattleBtn = ObjCreate.createGLoadedButton(ResFileConst.PetCpnHide, 357, 490, 121, 40, this);
            this._hideBattleBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onHideBattleBtnClickHandler);
            this._rewardSprite = new Object /* flash.display.Sprite */();
            this._rewardSprite.x = 285;
            this._rewardSprite.y = 315;
            this._rewardSprite.visible = false;
            this.addChild(this._rewardSprite);
            ObjCreate.createBg(0, 0, 350, 75, this._rewardSprite, ImagesConst.ToolTipBg);
            this._successTxt = ObjCreate.createTextField(Language.getString(54674), 0, 20, 300, 20, this._rewardSprite, GlobalStyle.textFormat6);
            this._successTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._rewardTxt = ObjCreate.createGTextField("", 0, 40, 350, 20, this._rewardSprite, GlobalStyle.textFormat6);
            this._rewardTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._rewardTxt.multiline = true;
            this._rewardSprite.addChild(this._rewardTxt);
            return;
        }

        /* internal  */updateMap(arg1: int): void
        {
            var mapId: int;

            var loc1;
            mapId = arg1;
            if (mapId == this._preMapId) 
            {
                return;
            }
            LoaderManager.instance.load(mapId + ".jpg", (arg1: ImageInfo): void
            {
                _mapBitmap.bitmapData = arg1.bitmapData;
                return;
            })
            return;
        }

        /* internal  */onHideBattleBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            if (this._hideBattleBtn.styleName == ResFileConst.PetCpnBack) 
            {
                GameController.petCompetition.view.show();
            }
            this.hide();
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            if (Cache.instance.petCompetition.isInBattle) 
            {
                this._hideBattleBtn.styleName = ResFileConst.PetCpnHide;
            }
            else 
            {
                this._hideBattleBtn.styleName = ResFileConst.PetCpnBack;
            }
            Cache.instance.petCompetition.battleIsShow = true;
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            Cache.instance.petCompetition.battleIsShow = false;
            this.exitWatch();
            this._rewardSprite.visible = false;
            if (this._effectPlayer && this.contains(this._effectPlayer)) 
            {
                this.removeChild(this._effectPlayer);
            }
            ThingUtil.sceneEntityUtil.removeEntityBySceneId(Cache.instance.petCompetition.mapId);
            return;
        }

        /* internal  */exitWatch(): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetCpnObserve, false));
            return;
        }

        public petCpnEntityUpdate(arg1: SEntityInfo): void
        {
            var loc1=false;
            this.updateMap(Cache.instance.petCompetition.mapId);
            var loc2=Cache.instance.role.entityInfo.entityId;
            loc1 = EntityUtil.petEqual(arg1.entityId, loc2);
            if (loc1) 
            {
                this._petAvatar.updateAvatarUI(arg1, true);
                if (this._petIEntity) 
                {
                    this._petIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onPetEntityInfoUpdateHandler);
                }
                this._petIEntity = ThingUtil.sceneEntityUtil.getEntity(arg1.entityId, Cache.instance.petCompetition.mapId);
                if (this._petIEntity) 
                {
                    this._petIEntity.addEventListener(PlayerEvent.UPDATEINFO, this.onPetEntityInfoUpdateHandler);
                }
            }
            else 
            {
                if (this._monsterIEntity) 
                {
                    this._monsterIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onMonsterEntityInfoUpdateHandler);
                }
                this._monsterIEntity = ThingUtil.sceneEntityUtil.getEntity(arg1.entityId, Cache.instance.petCompetition.mapId);
                if (this._monsterIEntity) 
                {
                    this._monsterIEntity.addEventListener(PlayerEvent.UPDATEINFO, this.onMonsterEntityInfoUpdateHandler);
                }
                this._enemyAvatar.updateAvatarUI(arg1, true);
            }
            return;
        }

        /* internal  */onPetEntityInfoUpdateHandler(arg1: PlayerEvent): void
        {
            var loc2=null;
            var loc1=arg1.player;
            if (loc1) 
            {
                loc2 = loc1.entityInfo.entityInfo;
                if (!(loc2.msgEx == null) && loc2.entityId && loc2.entityId.type == EEntityType._EEntityTypePetInArena) 
                {
                    this._petAvatar.updateAvatarUI(loc2, true);
                    if (loc1.isDead) 
                    {
                        this.removeAllPetByScene(Cache.instance.petCompetition.mapId);
                    }
                }
            }
            return;
        }

        public removeAllPetByScene(arg1: int): void
        {
            if (this._monsterIEntity) 
            {
                this._monsterIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onMonsterEntityInfoUpdateHandler);
            }
            if (this._petIEntity) 
            {
                this._petIEntity.removeEventListener(PlayerEvent.UPDATEINFO, this.onPetEntityInfoUpdateHandler);
            }
            return;
        }

        /* internal  */onMonsterEntityInfoUpdateHandler(arg1: PlayerEvent): void
        {
            var loc1=null;
            var loc3=null;
            var loc2=arg1.player;
            if (loc2 && loc2.entityInfo) 
            {
                if ((loc3 = loc2.entityInfo.entityInfo) && !(loc3.msgEx == null)) 
                {
                    this._enemyAvatar.updateAvatarUI(loc3, true);
                    if (loc2.isDead) 
                    {
                        this.removeAllPetByScene(Cache.instance.petCompetition.mapId);
                    }
                }
            }
            return;
        }

        /* internal */ /* var */_mapBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_petAvatar: PetExploreMainAvatar;

        /* internal */ /* var */_enemyAvatar: PetExploreMainAvatar;

        /* internal */ /* var */_levelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_hideBattleBtn: GLoadedButton;

        /* internal */ /* var */_effectPlayer: SWFPlayer;

        /* internal */ /* var */_rewardSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_successTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_rewardTxt: GTextFiled;

        /* internal */ /* var */_petIEntity: IEntity;

        /* internal */ /* var */_monsterIEntity: IEntity;

        /* internal */ /* var */_preMapId: int;

        /* internal */ /* var */_currentMonsterIndex: int;

        /* internal */ /* var */_zfItemData: ItemData;
    }
